package com.fortressweb.servlet;

import java.io.IOException;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.demoweb.model.dao.MembershipDao;
import com.demoweb.model.dto.Membership;

@WebServlet("/fortress/fortresspwsearch.nhn")
public class FortressPwSearchServlet extends HttpServlet {
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		resp.setContentType("text/html; charset=utf-8");
		
		// 1. 요청 데이터 읽기 (아이디, 비밀번호)
		String name = req.getParameter("name");
		String regNum = req.getParameter("regNum");
		
		System.out.println(name);
		System.out.println(regNum);
		
		// 2. 데이터 처리(db에서 조회)
		MembershipDao dao = new MembershipDao();
		Membership member = dao.getMemberByNameAndRegNum(name,regNum);
		
		
		// 3. member의 데이터 저장 하고 main으로 이동
		//( 로그인 성공 - member != NULL, 로그인 실패 - member == NULL )
		
		System.out.println("pw----" + member.getPassword());
		String path = null;
		
		if(member == null) { 
			path = "/demoweb/fortress/fortressloginfail.nhn";
		} else {
			HttpSession session = req.getSession();
			session.setAttribute("passwduser", member);
			
			if(req.getParameter("returnuri") != null) {
				path = req.getParameter("returnuri");
		} else {
			System.out.println("----------------------------------------");
				path = "/demoweb/fortress/fortressinformpw.nhn";
				
			}
		}

		resp.sendRedirect(path);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		doGet(req, resp);
	}
}


